package com.lxyk.controller.statistic;

import com.github.pagehelper.PageInfo;
import com.lxyk.domain.*;
import com.lxyk.service.statistic.StuAttendanceService;
import com.lxyk.service.student.StudentService;
//import org.apache.poi.xssf.usermodel.XSSFName;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

//学生到课率
@Controller
@RequestMapping("/statistic")
public class StuAttendanceController {
    @Autowired
    private StuAttendanceService stuAttendanceService;
    @Autowired
    private StudentService studentService;
    //班级当天所有学生到课率数据
    @RequestMapping("/classStdAttendanceDate")
    public String classStdAttendanceDate(StuStatisticVO condition, Integer pageNum, Integer pageSize, HttpServletRequest request){
        //查询当前班级最新的一次到课率日期
        Date lastDate = stuAttendanceService.getLastDate(condition.getCid());
        condition.setRecordDate(lastDate);
        return  findByConditionAttendance(condition,pageNum,pageSize,request);
    }

    //分页数据查询
    @RequestMapping("/classDayAttendanceByPage")
    public String classDayAttendanceByPage(StuStatisticVO condition,Integer pageNum,Integer pageSize, HttpServletRequest request){
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        PageInfo<StuStatisticVO> pageInfo = stuAttendanceService.findClassDayAttendance(condition.getCid(), condition.getRecordDate(), pageNum, pageSize);
        System.out.println(pageInfo);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("cid",condition.getCid());
        request.setAttribute("condition",condition);
        return  "data-dkedit";
    }

    //条件查询
    @RequestMapping("/findByConditionAttendance")
    public String findByConditionAttendance(StuStatisticVO stuStatisticVO,Integer pageNum,Integer pageSize, HttpServletRequest request){

        PageInfo<StuStatisticVO> pageInfo = stuAttendanceService.findByCondition(stuStatisticVO, pageNum, pageSize);
        System.out.println("pageInfo=" + pageInfo);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("cid", stuStatisticVO.getCid());
        request.setAttribute("condition",stuStatisticVO);
        return  "data-dkedit";
    }

    //更新(添加/修改)
    @RequestMapping("/saveAttendance")
    public String saveAttendance(StuStatisticVO2 stuStatisticVO2,StuStatisticVO condition,Integer pageNum,Integer pageSize,HttpServletRequest request){
//        System.out.println("stuStatisticVO2="+stuStatisticVO2);
        //获取所有要修改的数据
        List<StuStatisticVO> list = stuStatisticVO2.getList();
        //获取其中一个学生数据
        StuStatisticVO stuStatisticVO = list.get(0);
        //获取id
        Integer id = stuStatisticVO.getId();
        //获取班级id
        Integer cid = stuStatisticVO.getCid();
        //获取日期
        Date recordDate = stuStatisticVO.getRecordDate();
        stuAttendanceService.save(cid,recordDate,list);
        if(condition.getCid()==null){
            condition.setCid(cid);
        }
        request.setAttribute("condition",condition);
        String page = "";
        if(id!=null){
            //回到修改页
            page = findByConditionAttendance(condition,pageNum,pageSize,request);
//            page = classDayAttendanceByPage(cid, recordDate, pageNum, pageSize, request);
        }else{
            //回到添加页
            page = dkAddPage(condition,pageNum,pageSize,request);
        }
        request.setAttribute("success","OK");
        return page;
    }

    //跳转到添加页面
    @RequestMapping("/todkAdd")
    public String todkAdd(Integer cid,HttpServletRequest request){
        //跳转
        request.setAttribute("cid",cid);
        return "data-dkadd";
//        return dkAddPage(cid,1,10,request);
    }


    //添加页面,分页查询
    @RequestMapping("/dkAddPage")
    public String dkAddPage(StuStatisticVO condition,Integer pageNum,Integer pageSize,HttpServletRequest request){
        pageNum = pageNum!=null?pageNum:1;
        pageSize = pageSize!=null?pageSize:10;
        //根据班级id查询学生信息
        PageInfo<StuStatisticVO> pageInfo = stuAttendanceService.findStudentByCidAndDate(condition.getCid(),condition.getRecordDate(),pageNum, pageSize);
        System.out.println(pageInfo);
        //保存学生信息
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("cid",condition.getCid());
        request.setAttribute("condition",condition);
        return "data-dkadd";
    }

    //判断日期是否可用
    @RequestMapping("/isDateRight")
    @ResponseBody
    public String isDateRight(Integer cid,Date recordDate){
        List<StuStatisticVO> list = stuAttendanceService.findClassDayAttendance(cid, recordDate);
        if (list!=null && list.size()>0){
            //日期对应有数据,不可用
            return "false";
        }else{
            //日期对应没有数据,可用
            return "true";
        }
    }

    //上传excell文件
//    @RequestMapping("/uploadAttendance")
//    public String uploadAttendance(@RequestParam("filename")List<MultipartFile> files,HttpServletRequest request){
//        //判断上传文件是否为空
//        if(files!=null && files.size()>0){
//            for (MultipartFile file : files) {
//                try {
//                    InputStream inputStream = file.getInputStream();
//                    XSSFWorkbook book = new XSSFWorkbook(inputStream);
//                    List<XSSFName> allNames = book.getAllNames();
//                    System.out.println();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//
//            }
//        }
//
//        return "";
//    }
}
